﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;

namespace YArchitech.HVAC
{
	public class FittingSelectClass
	{
		public FamilySymbol GetFailySymbol(Autodesk.Revit.DB.Document doc, PartType partType)
		{
			IList<Element> fittings = this.GetFittings(doc, partType);
			FamilySymbol result = null;
			FittingSelect fittingSelect = new FittingSelect(fittings);
			if (fittingSelect.ShowDialog() == DialogResult.OK)
			{
				result = fittingSelect.familySymbol;
			}
			return result;
		}

		private IList<Element> GetFittings(Autodesk.Revit.DB.Document doc, PartType partType)
		{
			IEnumerable<Element> enumerable = new FilteredElementCollector(doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_DuctFitting).ToElements();
			IList<Element> list = new List<Element>();
			foreach (Element element in enumerable)
			{
				FamilySymbol familySymbol = element as FamilySymbol;
				FamilyInstance familyInstance = doc.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), familySymbol, (Autodesk.Revit.DB.Structure.StructuralType)0);
				if ((familyInstance.MEPModel as MechanicalFitting).PartType == partType)
				{
					list.Add(element);
				}
				doc.Delete(familyInstance.Id);
			}
			return list;
		}
	}
}
